package com.songcan.oauth.controller;


import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.songcan.common.annotation.JSON;
import com.songcan.common.exception.ObjectNotException;
import com.songcan.common.utils.TreeUtil;
import com.songcan.common.vo.MenuVo;
import com.songcan.model.oauth.SysMenu;
import com.songcan.oauth.service.SysMenuService;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@RequestMapping("rest/menu")
@RestController
@Validated
@AllArgsConstructor
public class MenuController {

     private final SysMenuService sysMenuService;




     @PostMapping()
     public void addMenu(@NotBlank(message = "名称不能为空")String name,
                         @NotBlank(message = "路由不能为空")String path, String icon,
                         String scope,@NotBlank(message = "url不能为空")String url,
                         @RequestParam(defaultValue = "0") Long parentId, String code,String method,
                         @RequestParam(defaultValue = "0")Integer type,String redirect,String component,
                         Integer status,String title,
                         @RequestParam(defaultValue = "0")Integer keepAlive,Integer sort){

         SysMenu sysMenu =  new SysMenu(IdWorker.getId(),name,scope,path,parentId,icon,code,sort,keepAlive,type,new Date(),null,0,method,url,redirect,component,status,title,null);
         sysMenuService.save(sysMenu);
     }


     @PutMapping()
    public void updateMenu(@NotNull(message = "id不能为空")Long id ,@NotBlank(message = "名称不能为空")String name,
                           @NotBlank(message = "路由不能为空")String path, String icon,String title,
                           String scope, Long parentId, String code, String method,Integer status,
                           @RequestParam(defaultValue = "0")Integer type,String url,String redirect,String component,
                           @RequestParam(defaultValue = "0")Integer keepAlive,Integer sort){
          SysMenu sysMenu = sysMenuService.getById(id);
          if(Objects.isNull(sysMenu)){
              throw new ObjectNotException("菜单不存在");
          }
          sysMenu.setName(name);
          sysMenu.setPath(path);
          sysMenu.setIcon(icon);
          sysMenu.setScope(scope);
          sysMenu.setParentId(parentId);
          sysMenu.setCode(code);
          sysMenu.setMethod(method);
          sysMenu.setType(type);
          sysMenu.setKeepAlive(keepAlive);
          sysMenu.setUrl(url);
          sysMenu.setSort(sort);
          sysMenu.setUpdateTime(new Date());
          sysMenu.setRedirect(redirect);
          sysMenu.setComponent(component);
          sysMenu.setStatus(status);
          sysMenu.setTitle(title);
          sysMenuService.update(sysMenu, Wrappers.lambdaUpdate(new SysMenu()).eq(SysMenu::getMenuId,id));
     }

     @GetMapping("/tree")
     @JSON(type = MenuVo.class,filter = "url,scope,role")
     public List<MenuVo> getByTree(){
         List<SysMenu> list = sysMenuService.list(Wrappers.lambdaQuery(new SysMenu()).eq(SysMenu::getDelFlag,0));
         return TreeUtil.buildByLoop(list.parallelStream().map(p->{
             MenuVo menuVo = new MenuVo();
             menuVo.setId(p.getMenuId());
             menuVo.setParentId(p.getParentId());
             menuVo.setPath(p.getPath());
             menuVo.setRedirect(p.getRedirect());
             menuVo.setComponent(p.getComponent());
             menuVo.setHidden(p.getStatus()==1);
             menuVo.setName(p.getName());
             MenuVo.Meta meta = new MenuVo.Meta();
             meta.setIcon(p.getIcon());
             meta.setTitle(p.getTitle());
             menuVo.setMeta(meta);
             return menuVo;
         }).collect(Collectors.toList()),list.get(0).getParentId());
     }

     @GetMapping()
    public SysMenu sysMenu(String id){
         return sysMenuService.getById(id);
     }

}
